﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Enterprise.Management.Business;
using Enterprise.Management.ViewModels;
using Enterprise.Management.Data;
using Enterprise.Management.Common;

namespace Enterprise.Management.Controllers
{
    public class ManagementController : BaseController
    {
        ManagementBLL mgr = new ManagementBLL();

        public ActionResult Index()
        {
            return View(mgr.GetUsers());
        }

        public ActionResult CreateUser()
        {
            CreateUserViewModel user = new CreateUserViewModel();
            user.Roles = new SelectList(mgr.GetRolesWithoutOwner(RoleType.Owner), "Role_ID", "Role_Description");
            user.Categories = new SelectList(mgr.GetCategories(DeptCategoryLevelType.LevelII), "Category_ID", "Category_Name");

            return View(user);
        }

        [HttpPost]
        public ActionResult CreateUser(CreateUserViewModel user)
        {
            user.Roles = new SelectList(mgr.GetRolesWithoutOwner(RoleType.Owner), "Role_ID", "Role_Description");
            user.Categories = new SelectList(mgr.GetCategories(DeptCategoryLevelType.LevelII), "Category_ID", "Category_Name");

            if(ModelState.IsValid)
            {
                if (user.User_RoleID == (int)RoleType.Manager && user.User_CtgrID == null)
                {
                    ModelState.AddModelError("User_CtgrID", "请指定分公司");
                    return View(user);
                }

                if (mgr.GetUser(user.User_Email) != null)
                {
                    ModelState.AddModelError("User_Email", "该邮箱已注册");
                    return View(user);
                }

                DP_User dp_user = new DP_User();
                if (user.User_RoleID == (int)RoleType.Manager)
                {
                    dp_user.User_CtgrID = user.User_CtgrID.Value;
                }
                else
                {
                    dp_user.User_CtgrID = 1;
                }
                
                dp_user.User_Name = user.User_Name;
                dp_user.User_Email = user.User_Email;
                dp_user.User_RoleID = user.User_RoleID;
                mgr.AddUser(dp_user);
            }

            return View(user);
        }

        public ActionResult CreateCatetory()
        {
            CreateCategoryViewModel category = new CreateCategoryViewModel();
            category.Categories = new SelectList(mgr.GetCategories(DeptCategoryLevelType.LevelII), "Category_ID", "Category_Name");

            return View(category);
        }

        [HttpPost]
        public ActionResult CreateCatetory(CreateCategoryViewModel category)
        {
            category.Categories = new SelectList(mgr.GetCategories(DeptCategoryLevelType.LevelII), "Category_ID", "Category_Name");

            if (ModelState.IsValid)
            {
                if (mgr.GetCategory(category.Category_Name) != null)
                {
                    ModelState.AddModelError("Category_Name", "该营业部已存在");
                    return View(category);
                }

                if (mgr.GetUser(category.User_Email) != null)
                {
                    ModelState.AddModelError("User_Email", "该邮箱已注册");
                    return View(category);
                }

                if (!category.Category_Name.Contains("营业部"))
                {
                    category.Category_Name += "营业部";
                }

                DP_Category dp_category = new DP_Category();
                dp_category.Category_Name = category.Category_Name;
                dp_category.Category_Parent = category.Category_ID;
                dp_category.Category_Level = (int)DeptCategoryLevelType.LevelIII;
                int ctgrID = mgr.AddCategory(dp_category);

                DP_User dp_user = new DP_User();
                dp_user.User_Name = category.Category_Name;
                dp_user.User_Email = category.User_Email;
                dp_user.User_RoleID = (int)RoleType.Owner;
                dp_user.User_CtgrID = ctgrID;
                mgr.AddUser(dp_user);
            }

            return View(category);
        }

        public ActionResult Edit(int id)
        {
            DP_User dp_user = mgr.GetUser(id);
            UpdateUserViewModel user = new UpdateUserViewModel();
            user.User_ID = dp_user.User_ID;
            user.Category_Level = dp_user.DP_Category.Category_Level;
            user.User_Email = dp_user.User_Email;
            user.User_Name = dp_user.User_Name;
            user.IsResetPwd = false;

            return View(user);
        }

        [HttpPost]
        public ActionResult Edit(UpdateUserViewModel user)
        {
            if (ModelState.IsValid)
            {
                mgr.UpdateUser(user);
            }
            return View(user);
        }

    }
}
